home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cgazv5n5.arc / LIST2.C < prev    next >
C/C++ Source or Header  |  1991-09-23  |  1KB  |  42 lines

  1. /*--------------------------------------- Listing 2 -------
  2.  * Demonstration of the signal() function.
  3.  * See Listing 1 for copyright terms.
  4.  *-------------------------------------------------------*/
  5. #include <stdio.h>
  6. #include <signal.h>
  7. #include <stdlib.h>
  8.  
  9. void ExcHandler ( int Signal, int SubCode );
  10. void main ( void );
  11.  
  12.  
  13. void ExcHandler ( int Signal, int SubCode )
  14. {
  15.     /* Ignore further user interrupts */
  16.     if ( signal ( SIGINT, SIG_IGN ) == SIG_ERR )
  17.         abort();
  18.  
  19.     printf ( "Signal %d, SubCode %d was raised\n", 
  20.              Signal, SubCode );
  21.  
  22.     /* Restore to our own exception handler */
  23.     if ( signal ( SIGINT, ExcHandler ) == SIG_ERR )
  24.         abort();
  25. }
  26.  
  27.  
  28. void main ( void )
  29. {
  30.     void (*OldHandler)( int Signal );
  31.  
  32.     OldHandler = signal ( SIGINT, ExcHandler );
  33.     if ( OldHandler == SIG_ERR )
  34.         printf( "signal install failed\n" );
  35.  
  36.     /* Explicitly raise the user-interrupt signal */
  37.     raise ( SIGINT );
  38.  
  39.     /* Now that we're done, go back to default signal handler */
  40.     if ( signal ( SIGINT, SIG_DFL ) == SIG_ERR )
  41.         abort();
  42. }